package com.scau.data.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scau.data.entity.StockDO;
import com.scau.database.entity.shopowner.ShopOwnerVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface StockMapper extends BaseMapper<StockDO> {

    /**
     * 通过商品条形码，获取库存大于0的仓库，以及仓库的地址
     * @param barCode 条形码
     * @return
     */
    @Select("SELECT stock.warehouse_id, stock.stock, warehouse.address " +
            "FROM stock INNER JOIN warehouse " +
            "ON stock.warehouse_id = warehouse.id WHERE " +
            "stock.bar_code = #{barCode} AND stock.stock > 0")
    List<ShopOwnerVO> getStores(String barCode);

    /**
     * 通过商品名，获取商品的条形码、余量
     * @param commodity 商品名
     * @return
     */
    @Select("SELECT stock.bar_code, stock.stock, order_line.commodity " +
            "FROM stock NATURAL JOIN order_line WHERE " +
            "order_line.commodity = #{commodity}")
    List<ShopOwnerVO> getStocks(String commodity);

    @Update("update stock set stock = stock + ${add} " +
            "where bar_code =#{barCode} " +
            "and warehouse_id =#{warehouseId}")
    Integer addStock(@Param("add") Integer add,
                     @Param("barCode") String barCode,
                     @Param("warehouseId") Integer warehouseId);
    @Update("update stock set stock = stock - ${reduce} " +
            "where bar_code =#{barCode} " +
            "and warehouse_id =#{warehouseId}")
    Integer reduceStock(@Param("reduce") Integer reduce,
                        @Param("barCode") String barCode,
                        @Param("warehouseId") Integer warehouseId);

}
